<template>
  <div class="avue-contail"
       :class="{'avue--collapse':isCollapse}">
    <screenshot />
    <div class="avue-header">
      <!-- 顶部导航栏 -->
      <top />
    </div>

    <div class="avue-layout">
      <div class="avue-left">
        <!-- 左侧导航栏 -->
        <sidebar />
      </div>
      <div class="avue-main">
        <!-- 顶部标签卡 -->
        <tags />
        <!-- 主体视图层 -->
        <el-scrollbar style="height:100%">
          <keep-alive>
            <router-view class="avue-view" 
                         v-if="$route.meta.keepAlive" />
          </keep-alive>
          <router-view class="avue-view"
                       v-if="!$route.meta.keepAlive" />
        </el-scrollbar>

      </div>
    </div>
    <!-- <el-footer class="avue-footer">
      <img src="/svg/logo.svg"
           alt=""
           class="logo">
      <p class="copyright">© 2018 Avue designed by smallwei</p>
    </el-footer> -->
    <div class="avue-shade"
         @click="showCollapse"></div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex';
import tags from './tags';
import top from './top/';
import screenshot from './screenshot';
import sidebar from './sidebar/';
import admin from '@/util/admin';
import {querySysOrgTree} from '@/api/system/sysOrgInfoApi';
import { reqCommonMsg } from '@/util/reqCommonMsg';
import {setStore, getStore} from '@/util/store'
import {queryGeneralOrgTree} from '@/api/general/staff/generalOrgInfoApi';
import {queryGeneralProjectInfoListPage} from '@/api/general/project/generalProjectInfoApi';

// import { validatenull } from "@/util/validate";
// import { calcDate } from "@/util/date.js";
// import { getStore } from "@/util/store.js";
export default {
  components: {
    top,
    tags,
    sidebar,
    screenshot
  },
  name: 'index',
  data() {
    return {
      //刷新token锁
      refreshLock: false,
      //刷新token的时间
      refreshTime: ''
    };
  },
  computed: mapGetters(['userInfo','isLock', 'isCollapse', 'website','orgTree','genOrgTree','isWarterMark']),
  created() {
    //初始化 用户store
    if(!this.setUserInfo){
      this.$store.dispatch('userInfo', getStore({name: 'userInfo'}));
    }
  },
  mounted() {
    this.init();
    //获取机构树形菜单
    setTimeout(() => {
      this.getOrgTree();
    }, 10);
    setTimeout(() => {
      this.getGeneralOrgTree();
    }, 1000);

    setTimeout(() => {
      this.getProjectList();
    }, 1000);
    
    //this.setWarterMark();
    //检查登录是否过期
    //有效期小于20分钟，强制刷新。
    setInterval(() => {
      this.userInfo.expiresIn -= 60;
      if(this.userInfo.expiresIn < 1200){
        this.$store.dispatch('RefreshToken', '').then(res => {
          //this.$console.log('2234567');
          this.$store.dispatch('setUserInfo', this.userInfo);
        }, error => {
        });
      }
    }, 60000);
  },
  props: [],
  methods: {
    /**
     * 设置水印
     */
    setWarterMark(){
      if(this.isWarterMark){
        this.isWarterMark.remove();
      }
      let obj = this.watermark({
        text: this.userInfo.companyName+'-'+this.userInfo.realName,
        textStyle: 'rgba(65,105,225,0.10)',
        degree: '-30',
        font: '20px 黑体'
      })
      this.$store.commit('IS_WARTER_MARK',obj);
    },
    /**
     * 获取树形菜单
     */
    getOrgTree(){
      // 加载树形机构列表
      this.singleBody = {
        orgNo: this.website.orgNo.treeOrgNo
      }
      this.reqParams =  {
        singleBody: this.singleBody,
        listBody: [{}],
        sqlCondition: this.conditions,
        orderRule: this.orderRules,
        currentPage: 1, 
        pageSize: 9999
      }
      this.commonData = reqCommonMsg(this.reqParams)
      querySysOrgTree(this.commonData).then(res => {
        this.$store.dispatch('setOrgTree', res.data.body.listBody)
      }, error => {
      });
    },
    /**
     * 获取树形菜单
     */
    getGeneralOrgTree(){
      // 加载树形机构列表
      this.singleBody = {
        orgNo: this.website.orgNo.genTreeOrgNo
      }
      this.reqParams =  {
        singleBody: this.singleBody,
        listBody: [{}],
        sqlCondition: this.conditions,
        orderRule: this.orderRules,
        currentPage: 1, 
        pageSize: 99999
      }
      this.commonData = reqCommonMsg(this.reqParams)
      queryGeneralOrgTree(this.commonData).then(res => {
        this.singleBody = {};
        this.$store.dispatch('setGenOrgTree', res.data.body.listBody)
      }, error => {
      });
    },
    /**
     * 获取项目列表
     */
    /**
     * 获取项目列表
     */
    getProjectList(){
      let singleBody = {
        tenantId: localStorage.getItem('tenantId')
      }
      let reqParams =  {
        singleBody: singleBody,
        currentPage: 1, 
        pageSize: 10000
      }
      let commonData = reqCommonMsg(reqParams)
      queryGeneralProjectInfoListPage(commonData).then(res => {
        const dataList = res.data.body.listBody;
        let projectList = [];
        dataList.map(item => {
          projectList.push({
            projectId: item.id,
            projectNo: item.projectNo,
            projectName: item.projectName
          });
        });
        this.$store.dispatch('setProjectList', projectList)
      }, error => {
        //this.$console.log(error);
      });
    },

    showCollapse() {
      this.$store.commit('SET_COLLAPSE');
    },
    // 屏幕检测
    init() {
      this.$store.commit('SET_SCREEN', admin.getScreen());
      window.onresize = () => {
        setTimeout(() => {
          this.$store.commit('SET_SCREEN', admin.getScreen());
        }, 0);
      };
    },
  }
};
</script>
